package com.problem.leetcode;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-05-07 19:31
 **/

public class Problem917 {
    public String reverseOnlyLetters(String S) {
        char[] chars = S.toCharArray();
        int    begin = 0, end = chars.length - 1;

        while (begin < end) {
            if (!Character.isAlphabetic(chars[begin])) {
                ++begin;
            } else if (!Character.isAlphabetic(chars[end])) {
                --end;
            } else {

                char tmp = chars[end];
                chars[end] = chars[begin];
                chars[begin] = tmp;
                ++begin;
                --end;

            }


        }
        return new String(chars);
    }
}
